package nullby.yoga.activity;

import java.util.ArrayList;
import java.util.List;

import nullby.yoga.R;
import nullby.yoga.adapter.ContentAdapter;
import nullby.yoga.bo.Content;
import nullby.yoga.utils.Extra;
import nullby.yoga.utils.SoundManager;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ContentsActivity extends CommonActivity implements OnClickListener {
	private static final String TAG = ContentsActivity.class.getSimpleName();

	private ListView lvProgramms;
	private List<Content> objects = new ArrayList<Content>();;
	private ArrayList<CharSequence> positionNames;
	private ArrayList<CharSequence> positionDescriptions;
	private ArrayList<CharSequence> positionIcons;

	private String programmId;
	private Dialog dialog;
	private FragmentManager fm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.contents);

		// programmId =
		// getIntent().getStringExtra(Extra.PROGRAMM_POSITIONS_NAMES);
		programmId = "1";
		positionNames = getIntent().getCharSequenceArrayListExtra(
				Extra.PROGRAMM_POSITIONS_NAMES);
		positionDescriptions = getIntent().getCharSequenceArrayListExtra(
				Extra.PROGRAMM_POSITIONS_DESCRIPTIONS);
		positionIcons = getIntent().getCharSequenceArrayListExtra(
				Extra.PROGRAMM_POSITIONS_ICONS);

		for (int i = 0; i < positionDescriptions.size(); i++) {
			objects.add(new Content(positionIcons.get(i), positionNames.get(i),
					positionDescriptions.get(i)));
		}
		initView();
		fm = getSupportFragmentManager();
	}

	@Override
	protected void initView() {
		lvProgramms = (ListView) findViewById(R.id.lvContents);

		// fillProgrammsList();

		lvProgramms.setAdapter(new ContentAdapter(ContentsActivity.this,
				R.layout.content_item, objects, programmId));
		lvProgramms.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				SoundManager.getInstance().playClick();
				Content content = objects.get(index);
				showAlert(ContentsActivity.this, content.getName().toString(),
						content.getDescription().toString());
				// Intent intent = new Intent(ContentsActivity.this,
				// HelpActivity.class);
				//
				// intent.putExtra(Extra.POSE_NAME, content.getName());
				// intent.putExtra(Extra.POSE_DESCRIPTION,
				// content.getDescription());
				//
				// startActivity(intent);
			}
		});
	}

	private void fillProgrammsList() {
		// String str = getString(R.string.list_programms);
		// try {
		// JSONObject jo = new JSONObject(str);
		// JSONArray ja = jo.getJSONArray("programms");
		// for (int i = 0; i < ja.length(); i++) {
		// objects.add(new Content((JSONObject) ja.get(i)));
		// }
		// } catch (JSONException e) {
		// Toast.makeText(ContentsActivity.this, "ERROR READ FILE PROGRAMMS",
		// 0).show();
		// }
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title:
			dialog.dismiss();
			break;
		case R.id.text:
			dialog.dismiss();
			break;
		default:
			break;
		}

	}

	public void showAlert(Context context, String title, String text) {

		dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog);

		if (!"".equals(title)) {
			TextView ttl = (TextView) dialog.findViewById(R.id.title);
			ttl.setVisibility(View.VISIBLE);
			ttl.setText(title);
			ttl.setOnClickListener(this);
		}
		TextView txt = (TextView) dialog.findViewById(R.id.text);
		txt.setText(text);
		txt.setOnClickListener(this);

		dialog.show();

		// int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		//
		// if (currentapiVersion > android.os.Build.VERSION_CODES.FROYO) {
		// HelpActivity ha = new HelpActivity();
		// Bundle bundle = new Bundle();
		// bundle.putString("text", text);
		// bundle.putString("title", title);
		// ha.setArguments(bundle);
		//
		// ha.show(fm, "help");
		//
		// } else {
		// AlertDialog.Builder builder = new AlertDialog.Builder(
		// new ContextThemeWrapper(context, R.style.AlertDialogCustom));
		// if (!"".equals(title)) {
		// builder.setTitle(title);
		// }
		// builder.setMessage(text);
		// AlertDialog alert = builder.create();
		// alert.show();
		// }
	}
}
